from flask import Flask, render_template, session, redirect, url_for, flash
from config01 import Config
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired

app = Flask(__name__)
app.config.from_object(Config)


class Form(FlaskForm):
    word = StringField(validators=[DataRequired()])
    submit = SubmitField('提交')


@app.route('/', methods=['GET', 'POST'])
def index():
    form = Form()
    # word = None
    if form.validate_on_submit():
        oldWord = session.get('word')
        if oldWord is not None and oldWord != form.word.data:
            flash('Looks like you have changed your name!')
        session['word'] = form.word.data
        # word = form.word.data
        # form.word.data = ''
        return redirect(url_for('index'))
    return render_template('01_formtest.html', form=form, word=session.get('word'))


if __name__ == '__main__':
    app.run()
